package com.bluestone.run;

import java.util.ArrayList;
import java.util.List;

import com.bluestone.context.ExecuteContext;
import com.bluestone.context.IContext;
import com.bluestone.context.ProjectContext;
import com.bluestone.scripts.Execute;
import com.bluestone.scripts.Project;
import com.bluestone.util.Util;

public class ProjectController extends BaseController {

	@Override
	boolean executeContext(IContext context) {
		ProjectContext projectContext = (ProjectContext)context;
		boolean flag = true;
		Project project = projectContext.getProject();
		if (project == null) {
			return false;
		}


		projectContext.addExecuteLog("run " + project.getName() + " start!");

		ArrayList executes = (ArrayList) project.getExecutes();
		ExecuteController controller = new ExecuteController();		
		for (int i = 0; i < executes.size(); i++) {
			Execute execute = (Execute) executes.get(i);
			ExecuteContext executeContext = projectContext.getExecuteContext(execute);
			boolean executeflag = controller.execute(executeContext);
			if (!executeflag) {
				flag = false;
				break;
			}
		}

		if (flag) {
			projectContext.addExecuteLog("run " + project.getName()
					+ " sucessful!");
		} else {
			projectContext.addExecuteLog("run " + project.getName()
					+ " failure!");
			Util.error("run " + project.getName() + " failure!");
		}
		
		return flag;
		
	}


}
